package com.bungarus.group.model;

import com.bungarus.group.cachestore.CacheStore;
import com.bungarus.group.cachestore.CacheTransformer;
import com.bungarus.group.cachestore.StoreColumn;
import com.bungarus.group.cachestore.StoreRow;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class MemberGroupCacheTransformer implements CacheTransformer<List<GroupMember>> {
    private static final String NAME = "membergroup";
    @Override
    public int getCachedAction() {
        return CacheTransformer.CACHE_ACTION_SORTED_SET;
    }

    @Override
    public String getCacheTableName() {
        return NAME;
    }

    @Override
    public StoreRow transform(List<GroupMember> source) {
        if(null != source && source.size() > 0) {
            Set<StoreColumn> columns = new LinkedHashSet<>();
            source.forEach(gm -> {
                StoreColumn column = new StoreColumn();
                column.setColumnName(String.valueOf(gm.getGid()));
                column.setColumnValue(String.valueOf(gm.getGid()));

                columns.add(column);
            });
            StoreRow.Builder builder = StoreRow.newBuilder();
            return builder.table(getCacheTableName())
                    .id(CacheStore.ofUniqueId(
                            String.valueOf(source.get(0).getTenantId()),
                            String.valueOf(source.get(0).getMid())))
                    .values(columns)
                    .build();
        }
        return null;
    }

    @Override
    public List<StoreRow> transform2List(List<GroupMember> source) {
        if(null != source && source.size() > 0) {
            return source.stream()
                    .map(groupMember -> {
                        StoreColumn column = new StoreColumn();
                        column.setColumnName(String.valueOf(groupMember.getGid()));
                        column.setColumnValue(String.valueOf(groupMember.getGid()));

                        Set<StoreColumn> values = new LinkedHashSet<>(1);
                        values.add(column);

                        StoreRow.Builder builder = StoreRow.newBuilder();
                        return builder
                                .table(getCacheTableName())
                                .id(CacheStore.ofUniqueId(
                                        String.valueOf(groupMember.getTenantId()),
                                        String.valueOf(groupMember.getMid())))
                                .values(values)
                                .build();
                    })
                    .collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public List<GroupMember> reverse(StoreRow row) {
        String uniqueRowId = row.getRowUniqueKeyValue();
        //pattern-> tenantId:memberId
        String[] values = StringUtils.tokenizeToStringArray(uniqueRowId, StoreRow.SEPERATOR);

        Set<StoreColumn> columns = row.getRowValues();
        List<GroupMember> gms = new LinkedList<>();
        columns.forEach(column -> {
            GroupMember gm = new GroupMember();
            gm.setTenantId(Long.valueOf(values[0]));
            gm.setMid(Long.valueOf(values[1]));
            gm.setGid(Long.valueOf(column.getColumnValue()));
            gms.add(gm);
        });
        return gms;
    }
}
